class PicturesController < ApplicationController
  # GET /pictures
  # GET /pictures.xml
  def index
    @pictures = Picture.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @pictures }
    end
  end

  # GET /pictures/1
  # GET /pictures/1.xml
  def show
    @picture = Picture.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @picture }
    end
  end

  # GET /pictures/new
  # GET /pictures/new.xml
  def new
    @picture = Picture.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @picture }
    end
  end

  # GET /pictures/1/edit
  def edit
    @picture = Picture.find(params[:id])
  end

  # POST /pictures
  # POST /pictures.xml
  def create
    @picture = Picture.new(params[:picture])

    respond_to do |format|
      if @picture.save
        flash[:notice] = 'Picture was successfully created.'
        format.html { redirect_to(@picture) }
        format.xml  { render :xml => @picture, :status => :created, :location => @picture }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @picture.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /pictures/1
  # PUT /pictures/1.xml
  def update
    @picture = Picture.find(params[:id])

    respond_to do |format|
      if @picture.update_attributes(params[:picture])
        flash[:notice] = 'Picture was successfully updated.'
        format.html { redirect_to(@picture) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @picture.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /pictures/1
  # DELETE /pictures/1.xml
  def destroy
    @picture = Picture.find(params[:id])
    @picture.destroy

    respond_to do |format|
      format.html { redirect_to(pictures_url) }
      format.xml  { head :ok }
    end
  end

  def crawl
#    input = params[:domain_name]
#    inputs = input.split(/;/)
#    @domain_name = inputs[0].to_s
#    @amount = inputs[1].to_i
#    puts @domain_name
#    puts @amount
    @domain_name = params[:domain_name]
    h = Net::HTTP.new(@domain_name, 80)
    resp, data = h.get('/', nil)
    @pictures = Array.new
    if resp.message == "OK"
      @urls = data.scan(/(<img [^>^<]*src="http:[^\s^>^<]*[jpg|gif|jpeg|png]"[^>^<]*>)/)
      @urls.each do |url|
        puts '----------------------------------------------------------------'
        width = 0;
        height = 0;
        if (url.to_s =~ /.*width="([\d]*)".*/) == 0
          width = $1.to_i
        end
        if (url.to_s =~ /.*height="([\d]*)".*/) == 0
          height = $1.to_i
        end
        puts width
        puts height
        if width>20&&height>20
          url.to_s =~ /<img [^>^<]*src="(http:[^\s^>^<]*[jpg|gif|jpeg|png])"[^>^<]*>/

          puts $1
            @picture = Picture.new
            @picture.url = $1.to_s
            @picture.save
            @pictures << @picture
        end
      end
    puts '----------------------------------------------------------------'
    end
    h.close_on_empty_response
    render :partial => 'picture',:collection => @pictures
  end


  def test
    puts Picture.get_random_picture.url
  end

  def showpics
    @pictures = session[:pictures]
  end

end
